package com.freedom.rpc.thrift.registry;

import java.util.ArrayList;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.freedom.rpc.thrift.common.utils.LoggerUtils;
import com.freedom.rpc.thrift.server.utils.MyServerProperties;
import com.freedom.rpc.thrift.server.utils.TimeUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisPoolManager {
	//
	// 私有成员
	private JedisPoolManager() {
	}

	private ArrayList<JedisPool> jedisPools = new ArrayList<JedisPool>();

	public ArrayList<JedisPool> getJedisPools() {
		return jedisPools;
	}

	private void addJedisPool(JedisPool pool) {
		jedisPools.add(pool);
	}

	//
	// 下面是静态类
	private static final Logger logger = LogManager.getLogger(JedisPoolManager.class);
	private static JedisPoolManager instance = null;

	public static JedisPoolManager getInstance() {
		return instance;
	}

	static {
		// 参考文档:
		// http://www.cnblogs.com/tankaixiong/p/4048167.html
		//
		LoggerUtils.info(logger, "begin to init jedis pool...");
		// 初始化instance
		instance = new JedisPoolManager();
		// 准备好配置
		JedisPoolConfig config = new JedisPoolConfig();
		// config.setFairness(fairness);
		config.setMaxIdle(6);
		config.setMinIdle(2);
		config.setMaxTotal(10);
		config.setLifo(false);
		// 有效性校验
		config.setTestOnBorrow(true);
		config.setTestOnCreate(true);
		config.setTestOnReturn(true);
		config.setTestWhileIdle(true);
		// 设置获取资源的等待时长
		config.setBlockWhenExhausted(true);
		config.setMaxWaitMillis(3000);// 等待3秒

		// 逐出扫描
		config.setTimeBetweenEvictionRunsMillis(6000);// 逐出扫描的时间间隔
		config.setMinEvictableIdleTimeMillis(1 * 60 * 1000);// 1分钟
		config.setSoftMinEvictableIdleTimeMillis(1 * 60 * 1000);// 1分钟
		config.setNumTestsPerEvictionRun(6);// 每次逐出的最大数量
		// config.setEvictionPolicyClassName(evictionPolicyClassName);
		//
		String[] redisServers = MyServerProperties.getInstance().getRedisServers();
		for (String redisServer : redisServers) {
			redisServer = redisServer.trim();
			String[] ipPort = redisServer.split(":");
			JedisPool pool = new JedisPool(config, ipPort[0], Integer.parseInt(ipPort[1]));
			instance.addJedisPool(pool);
		}
	}

	public static void main(String[] args) {
		while (true) {
			JedisPoolManager manager = JedisPoolManager.getInstance();
			ArrayList<JedisPool> jedisPools = manager.getJedisPools();
			for (JedisPool pool : jedisPools) {
				int active = pool.getNumIdle();
				LoggerUtils.info(logger, "active: " + active);
				Jedis jedis = null;
				// 这里可能获取资源失败
				try {
					jedis = pool.getResource();
				} catch (Exception e) {
					LoggerUtils.error(logger, e.toString());
					jedis = null;
				}
				if (null == jedis) {
					continue;
				}
				LoggerUtils.info(logger, "succeed to get a jedis instance");
				//TimeUtils.sleepSeconds(6);
				// 操作---异常
				try {
					jedis.publish("key", "value");
					pool.returnResource(jedis);
				} catch (Exception e) {
					LoggerUtils.error(logger, e.toString());
					pool.returnBrokenResource(jedis);
				}
				// 结束
			}
			TimeUtils.sleepSeconds(1);
		}
	}
}
